<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="language" content="en" />
        <link href="./assets/ffd55088/css/bootstrap.css" rel="stylesheet">
<link href="./assets/5cf9384a/solarized_light.css" rel="stylesheet">
<link href="./assets/6c54116e/style.css" rel="stylesheet">
<script src="./assets/a44cef0f/jquery.js"></script>
<script src="./assets/ffd55088/js/bootstrap.js"></script>
<script src="./assets/8ac4e28a/jssearch.js"></script>    <title>Mailing - Special Topics - The Definitive Guide to Yii 2.0</title>
</head>
<body>

<div class="wrap">
    <nav id="w206" class="navbar-inverse navbar-fixed-top navbar" role="navigation"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w206-collapse"><span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span></button><a class="navbar-brand" href="./index.html">The Definitive Guide to Yii 2.0</a></div><div id="w206-collapse" class="collapse navbar-collapse"><ul id="w207" class="navbar-nav nav"><li><a href="./index.html">Class reference</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Extensions <b class="caret"></b></a><ul id="w208" class="dropdown-menu"><li><a href="./ext-apidoc-index.html" tabindex="-1">apidoc</a></li>
<li><a href="./ext-authclient-index.html" tabindex="-1">authclient</a></li>
<li><a href="./ext-bootstrap-index.html" tabindex="-1">bootstrap</a></li>
<li><a href="./ext-codeception-index.html" tabindex="-1">codeception</a></li>
<li><a href="./ext-debug-index.html" tabindex="-1">debug</a></li>
<li><a href="./ext-elasticsearch-index.html" tabindex="-1">elasticsearch</a></li>
<li><a href="./ext-faker-index.html" tabindex="-1">faker</a></li>
<li><a href="./ext-gii-index.html" tabindex="-1">gii</a></li>
<li><a href="./ext-imagine-index.html" tabindex="-1">imagine</a></li>
<li><a href="./ext-jui-index.html" tabindex="-1">jui</a></li>
<li><a href="./ext-mongodb-index.html" tabindex="-1">mongodb</a></li>
<li><a href="./ext-redis-index.html" tabindex="-1">redis</a></li>
<li><a href="./ext-smarty-index.html" tabindex="-1">smarty</a></li>
<li><a href="./ext-sphinx-index.html" tabindex="-1">sphinx</a></li>
<li><a href="./ext-swiftmailer-index.html" tabindex="-1">swiftmailer</a></li>
<li><a href="./ext-twig-index.html" tabindex="-1">twig</a></li></ul></li>
<li><a href="./guide-README.html">Guide</a></li></ul><div class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input id="searchbox" type="text" class="form-control" placeholder="Search">
  </div>
</div>
</div></nav>
    <div id="search-resultbox" style="display: none;" class="modal-content">
        <ul id="search-results">
        </ul>
    </div>

    
<div class="row">
    <div class="col-md-2">
                <div id="navigation" class="list-group"><a class="list-group-item" href="#navigation-190" data-toggle="collapse" data-parent="#navigation">Introduction <b class="caret"></b></a><div id="navigation-190" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-intro-yii.html">About Yii</a>
<a class="list-group-item" href="./guide-intro-upgrade-from-v1.html">Upgrading from Version 1.1</a></div>
<a class="list-group-item" href="#navigation-191" data-toggle="collapse" data-parent="#navigation">Getting Started <b class="caret"></b></a><div id="navigation-191" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-start-installation.html">Installing Yii</a>
<a class="list-group-item" href="./guide-start-workflow.html">Running Applications</a>
<a class="list-group-item" href="./guide-start-hello.html">Saying Hello</a>
<a class="list-group-item" href="./guide-start-forms.html">Working with Forms</a>
<a class="list-group-item" href="./guide-start-databases.html">Working with Databases</a>
<a class="list-group-item" href="./guide-start-gii.html">Generating Code with Gii</a>
<a class="list-group-item" href="./guide-start-looking-ahead.html">Looking Ahead</a></div>
<a class="list-group-item" href="#navigation-192" data-toggle="collapse" data-parent="#navigation">Application Structure <b class="caret"></b></a><div id="navigation-192" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-structure-overview.html">Overview</a>
<a class="list-group-item" href="./guide-structure-entry-scripts.html">Entry Scripts</a>
<a class="list-group-item" href="./guide-structure-applications.html">Applications</a>
<a class="list-group-item" href="./guide-structure-application-components.html">Application Components</a>
<a class="list-group-item" href="./guide-structure-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-structure-models.html">Models</a>
<a class="list-group-item" href="./guide-structure-views.html">Views</a>
<a class="list-group-item" href="./guide-structure-modules.html">Modules</a>
<a class="list-group-item" href="./guide-structure-filters.html">Filters</a>
<a class="list-group-item" href="./guide-structure-widgets.html">Widgets</a>
<a class="list-group-item" href="./guide-structure-assets.html">Assets</a>
<a class="list-group-item" href="./guide-structure-extensions.html">Extensions</a></div>
<a class="list-group-item" href="#navigation-193" data-toggle="collapse" data-parent="#navigation">Handling Requests <b class="caret"></b></a><div id="navigation-193" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-runtime-overview.html">Overview</a>
<a class="list-group-item" href="./guide-runtime-bootstrapping.html">Bootstrapping</a>
<a class="list-group-item" href="./guide-runtime-routing.html">Routing and URL Creation</a>
<a class="list-group-item" href="./guide-runtime-requests.html">Requests</a>
<a class="list-group-item" href="./guide-runtime-responses.html">Responses</a>
<a class="list-group-item" href="./guide-runtime-sessions-cookies.html">Sessions and Cookies</a>
<a class="list-group-item" href="./guide-runtime-handling-errors.html">Handling Errors</a>
<a class="list-group-item" href="./guide-runtime-logging.html">Logging</a></div>
<a class="list-group-item" href="#navigation-194" data-toggle="collapse" data-parent="#navigation">Key Concepts <b class="caret"></b></a><div id="navigation-194" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-concept-components.html">Components</a>
<a class="list-group-item" href="./guide-concept-properties.html">Properties</a>
<a class="list-group-item" href="./guide-concept-events.html">Events</a>
<a class="list-group-item" href="./guide-concept-behaviors.html">Behaviors</a>
<a class="list-group-item" href="./guide-concept-configurations.html">Configurations</a>
<a class="list-group-item" href="./guide-concept-aliases.html">Aliases</a>
<a class="list-group-item" href="./guide-concept-autoloading.html">Class Autoloading</a>
<a class="list-group-item" href="./guide-concept-service-locator.html">Service Locator</a>
<a class="list-group-item" href="./guide-concept-di-container.html">Dependency Injection Container</a></div>
<a class="list-group-item" href="#navigation-195" data-toggle="collapse" data-parent="#navigation">Working with Databases <b class="caret"></b></a><div id="navigation-195" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-db-dao.html">Data Access Objects</a>
<a class="list-group-item" href="./guide-db-query-builder.html">Query Builder</a>
<a class="list-group-item" href="./guide-db-active-record.html">Active Record</a>
<a class="list-group-item" href="./guide-db-migrations.html">Migrations</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-sphinx/blob/master/docs/guide/README.md">Sphinx</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide/README.md">Redis</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-mongodb/blob/master/docs/guide/README.md">MongoDB</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-elasticsearch/blob/master/docs/guide/README.md">ElasticSearch</a></div>
<a class="list-group-item" href="#navigation-196" data-toggle="collapse" data-parent="#navigation">Getting Data from Users <b class="caret"></b></a><div id="navigation-196" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-input-forms.html">Creating Forms</a>
<a class="list-group-item" href="./guide-input-validation.html">Validating Input</a>
<a class="list-group-item" href="./guide-input-file-upload.html">Uploading Files</a>
<a class="list-group-item" href="./guide-input-tabular-input.html">Collecting Tabular Input</a>
<a class="list-group-item" href="./guide-input-multiple-models.html">Getting Data for Multiple Models</a></div>
<a class="list-group-item" href="#navigation-197" data-toggle="collapse" data-parent="#navigation">Displaying Data <b class="caret"></b></a><div id="navigation-197" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-output-formatting.html">Data Formatting</a>
<a class="list-group-item" href="./guide-output-pagination.html">Pagination</a>
<a class="list-group-item" href="./guide-output-sorting.html">Sorting</a>
<a class="list-group-item" href="./guide-output-data-providers.html">Data Providers</a>
<a class="list-group-item" href="./guide-output-data-widgets.html">Data Widgets</a>
<a class="list-group-item" href="./guide-output-client-scripts.html">Working with Client Scripts</a>
<a class="list-group-item" href="./guide-output-theming.html">Theming</a></div>
<a class="list-group-item" href="#navigation-198" data-toggle="collapse" data-parent="#navigation">Security <b class="caret"></b></a><div id="navigation-198" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-security-overview.html">Overview</a>
<a class="list-group-item" href="./guide-security-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-security-authorization.html">Authorization</a>
<a class="list-group-item" href="./guide-security-passwords.html">Working with Passwords</a>
<a class="list-group-item" href="./guide-security-cryptography.html">Cryptography</a>
<a class="list-group-item" href="./guide-structure-views.html#security">Views security</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-authclient/blob/master/docs/guide/README.md">Auth Clients</a>
<a class="list-group-item" href="./guide-security-best-practices.html">Best Practices</a></div>
<a class="list-group-item" href="#navigation-199" data-toggle="collapse" data-parent="#navigation">Caching <b class="caret"></b></a><div id="navigation-199" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-caching-overview.html">Overview</a>
<a class="list-group-item" href="./guide-caching-data.html">Data Caching</a>
<a class="list-group-item" href="./guide-caching-fragment.html">Fragment Caching</a>
<a class="list-group-item" href="./guide-caching-page.html">Page Caching</a>
<a class="list-group-item" href="./guide-caching-http.html">HTTP Caching</a></div>
<a class="list-group-item" href="#navigation-200" data-toggle="collapse" data-parent="#navigation">RESTful Web Services <b class="caret"></b></a><div id="navigation-200" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-rest-quick-start.html">Quick Start</a>
<a class="list-group-item" href="./guide-rest-resources.html">Resources</a>
<a class="list-group-item" href="./guide-rest-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-rest-routing.html">Routing</a>
<a class="list-group-item" href="./guide-rest-response-formatting.html">Response Formatting</a>
<a class="list-group-item" href="./guide-rest-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-rest-rate-limiting.html">Rate Limiting</a>
<a class="list-group-item" href="./guide-rest-versioning.html">Versioning</a>
<a class="list-group-item" href="./guide-rest-error-handling.html">Error Handling</a></div>
<a class="list-group-item" href="#navigation-201" data-toggle="collapse" data-parent="#navigation">Development Tools <b class="caret"></b></a><div id="navigation-201" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md">Debug Toolbar and Debugger</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/README.md">Generating Code using Gii</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-apidoc">Generating API Documentation</a></div>
<a class="list-group-item" href="#navigation-202" data-toggle="collapse" data-parent="#navigation">Testing <b class="caret"></b></a><div id="navigation-202" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-test-overview.html">Overview</a>
<a class="list-group-item" href="./guide-test-environment-setup.html">Testing environment setup</a>
<a class="list-group-item" href="./guide-test-unit.html">Unit Tests</a>
<a class="list-group-item" href="./guide-test-functional.html">Functional Tests</a>
<a class="list-group-item" href="./guide-test-acceptance.html">Acceptance Tests</a>
<a class="list-group-item" href="./guide-test-fixtures.html">Fixtures</a></div>
<a class="list-group-item active" href="#navigation-203" data-toggle="collapse" data-parent="#navigation">Special Topics <b class="caret"></b></a><div id="navigation-203" class="submenu panel-collapse collapse in"><a class="list-group-item" href="https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/README.md">Advanced Project Template</a>
<a class="list-group-item" href="./guide-tutorial-start-from-scratch.html">Building Application from Scratch</a>
<a class="list-group-item" href="./guide-tutorial-console.html">Console Commands</a>
<a class="list-group-item" href="./guide-tutorial-core-validators.html">Core Validators</a>
<a class="list-group-item" href="./guide-tutorial-i18n.html">Internationalization</a>
<a class="list-group-item active" href="./guide-tutorial-mailing.html">Mailing</a>
<a class="list-group-item" href="./guide-tutorial-performance-tuning.html">Performance Tuning</a>
<a class="list-group-item" href="./guide-tutorial-shared-hosting.html">Shared Hosting Environment</a>
<a class="list-group-item" href="./guide-tutorial-template-engines.html">Template Engines</a>
<a class="list-group-item" href="./guide-tutorial-yii-integration.html">Working with Third-Party Code</a></div>
<a class="list-group-item" href="#navigation-204" data-toggle="collapse" data-parent="#navigation">Widgets <b class="caret"></b></a><div id="navigation-204" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-bootstrap/blob/master/docs/guide/README.md">Bootstrap Widgets</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-jui/blob/master/docs/guide/README.md">jQuery UI Widgets</a></div>
<a class="list-group-item" href="#navigation-205" data-toggle="collapse" data-parent="#navigation">Helpers <b class="caret"></b></a><div id="navigation-205" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-helper-overview.html">Overview</a>
<a class="list-group-item" href="./guide-helper-array.html">ArrayHelper</a>
<a class="list-group-item" href="./guide-helper-html.html">Html</a>
<a class="list-group-item" href="./guide-helper-url.html">Url</a></div></div>    </div>
    <div class="col-md-9 guide-content" role="main">
        <h1>Mailing <span id="mailing"></span><a href="#mailing" class="hashlink">&para;</a></h1>
<div class="toc"><ol><li><a href="#configuration">Configuration</a></li>
<li><a href="#basic-usage">Basic usage</a></li>
<li><a href="#composing-mail-content">Composing mail content</a></li>
<li><a href="#file-attachment">File attachment</a></li>
<li><a href="#embedding-images">Embedding images</a></li>
<li><a href="#testing-and-debugging">Testing and debugging</a></li>
<li><a href="#creating-your-own-mail-solution">Creating your own mail solution</a></li></ol></div>
<blockquote class="note"><p><strong>Note: </strong>This section is under development.</p>
</blockquote>
<p>Yii supports composition and sending of the email messages. However, the framework core provides
only the content composition functionality and basic interface. Actual mail sending mechanism should
be provided by the extension, because different projects may require its different implementation and
it usually depends on the external services and libraries.</p>
<p>For the most common cases you can use <a href="https://github.com/yiisoft/yii2-swiftmailer">yii2-swiftmailer</a> official extension.</p>
<h2>Configuration <span id="configuration"></span><a href="#configuration" class="hashlink">&para;</a></h2><p>Mail component configuration depends on the extension you have chosen.
In general your application configuration should look like:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">return</span> [
    <span class="hljs-comment">//....</span>
    <span class="hljs-string">'components'</span> =&gt; [
        <span class="hljs-string">'mailer'</span> =&gt; [
            <span class="hljs-string">'class'</span> =&gt; <span class="hljs-string">'yii\swiftmailer\Mailer'</span>,
        ],
    ],
];
</code></pre>
<h2>Basic usage <span id="basic-usage"></span><a href="#basic-usage" class="hashlink">&para;</a></h2><p>Once the 'mailer' component is configured, you can use the following code to send an email message:</p>
<pre><code class="hljs php language-php">Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;compose()
    -&gt;setFrom(<span class="hljs-string">'from@domain.com'</span>)
    -&gt;setTo(<span class="hljs-string">'to@domain.com'</span>)
    -&gt;setSubject(<span class="hljs-string">'Message subject'</span>)
    -&gt;setTextBody(<span class="hljs-string">'Plain text content'</span>)
    -&gt;setHtmlBody(<span class="hljs-string">'&lt;b&gt;HTML content&lt;/b&gt;'</span>)
    -&gt;send();
</code></pre>
<p>In the above example the method <code>compose()</code> creates an instance of the mail message, which then is populated and sent.
You may put more complex logic in this process if needed:</p>
<pre><code class="hljs php language-php"><span class="hljs-variable">$message</span> = Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;compose();
<span class="hljs-keyword">if</span> (Yii::<span class="hljs-variable">$app</span>-&gt;user-&gt;isGuest) {
    <span class="hljs-variable">$message</span>-&gt;setFrom(<span class="hljs-string">'from@domain.com'</span>)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-variable">$message</span>-&gt;setFrom(Yii::<span class="hljs-variable">$app</span>-&gt;user-&gt;identity-&gt;email)
}
<span class="hljs-variable">$message</span>-&gt;setTo(Yii::<span class="hljs-variable">$app</span>-&gt;params[<span class="hljs-string">'adminEmail'</span>])
    -&gt;setSubject(<span class="hljs-string">'Message subject'</span>)
    -&gt;setTextBody(<span class="hljs-string">'Plain text content'</span>)
    -&gt;send();
</code></pre>
<blockquote class="note"><p><strong>Note: </strong>each 'mailer' extension comes in 2 major classes: 'Mailer' and 'Message'. 'Mailer' always knows
  the class name and specific of the 'Message'. Do not attempt to instantiate 'Message' object directly -
  always use <code>compose()</code> method for it.</p>
</blockquote>
<p>You may also send several messages at once:</p>
<pre><code class="hljs php language-php"><span class="hljs-variable">$messages</span> = [];
<span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$users</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$user</span>) {
    <span class="hljs-variable">$messages</span>[] = Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;compose()
        <span class="hljs-comment">// ...</span>
        -&gt;setTo(<span class="hljs-variable">$user</span>-&gt;email);
}
Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;sendMultiple(<span class="hljs-variable">$messages</span>);
</code></pre>
<p>Some particular mail extensions may benefit from this approach, using single network message etc.</p>
<h2>Composing mail content <span id="composing-mail-content"></span><a href="#composing-mail-content" class="hashlink">&para;</a></h2><p>Yii allows composition of the actual mail messages content via special view files.
By default these files should be located at '@app/mail' path.</p>
<p>Example mail view file content:</p>
<pre><code class="hljs php language-php"><span class="hljs-preprocessor">&lt;?php</span>
<span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">helpers</span>\<span class="hljs-title">Html</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">helpers</span>\<span class="hljs-title">Url</span>;


<span class="hljs-comment">/* <span class="hljs-doctag">@var</span> $this \yii\web\View view component instance */</span>
<span class="hljs-comment">/* <span class="hljs-doctag">@var</span> $message \yii\mail\BaseMessage instance of newly created mail message */</span>

<span class="hljs-preprocessor">?&gt;</span>
&lt;h2&gt;This message allows you to visit our site home page by one click&lt;/h2&gt;
<span class="hljs-preprocessor">&lt;?</span>= Html::a(<span class="hljs-string">'Go to home page'</span>, Url::home(<span class="hljs-string">'http'</span>)) <span class="hljs-preprocessor">?&gt;</span>
</code></pre>
<p>In order to compose message content via view file simply pass view name to the <code>compose()</code> method:</p>
<pre><code class="hljs php language-php">Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;compose(<span class="hljs-string">'home-link'</span>) <span class="hljs-comment">// a view rendering result becomes the message body here</span>
    -&gt;setFrom(<span class="hljs-string">'from@domain.com'</span>)
    -&gt;setTo(<span class="hljs-string">'to@domain.com'</span>)
    -&gt;setSubject(<span class="hljs-string">'Message subject'</span>)
    -&gt;send();
</code></pre>
<p>You may pass additional view parameters to <code>compose()</code> method, which will be available inside the view files:</p>
<pre><code class="hljs php language-php">Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;compose(<span class="hljs-string">'greetings'</span>, [
    <span class="hljs-string">'user'</span> =&gt; Yii::<span class="hljs-variable">$app</span>-&gt;user-&gt;identity,
    <span class="hljs-string">'advertisement'</span> =&gt; <span class="hljs-variable">$adContent</span>,
]);
</code></pre>
<p>You can specify different view files for HTML and plain text message contents:</p>
<pre><code class="hljs php language-php">Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;compose([
    <span class="hljs-string">'html'</span> =&gt; <span class="hljs-string">'contact-html'</span>,
    <span class="hljs-string">'text'</span> =&gt; <span class="hljs-string">'contact-text'</span>,
]);
</code></pre>
<p>If you specify view name as a scalar string, its rendering result will be used as HTML body, while
plain text body will be composed by removing all HTML entities from HTML one.</p>
<p>View rendering result can be wrapped into the layout, which can be setup using <a href="./yii-mail-basemailer.html#$htmlLayout-detail">yii\mail\BaseMailer::$htmlLayout</a>
and <a href="./yii-mail-basemailer.html#$textLayout-detail">yii\mail\BaseMailer::$textLayout</a>. It will work the same way like layouts in regular web application.
Layout can be used to setup mail CSS styles or other shared content:</p>
<pre><code class="hljs php language-php"><span class="hljs-preprocessor">&lt;?php</span>
<span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">helpers</span>\<span class="hljs-title">Html</span>;

<span class="hljs-comment">/* <span class="hljs-doctag">@var</span> $this \yii\web\View view component instance */</span>
<span class="hljs-comment">/* <span class="hljs-doctag">@var</span> $message \yii\mail\MessageInterface the message being composed */</span>
<span class="hljs-comment">/* <span class="hljs-doctag">@var</span> $content string main view render result */</span>
<span class="hljs-preprocessor">?&gt;</span>
<span class="hljs-preprocessor">&lt;?php</span> <span class="hljs-variable">$this</span>-&gt;beginPage() <span class="hljs-preprocessor">?&gt;</span>
&lt;!DOCTYPE html <span class="hljs-keyword">PUBLIC</span> <span class="hljs-string">"-//W3C//DTD XHTML 1.0 Strict//EN"</span> <span class="hljs-string">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"</span>&gt;
&lt;html xmlns=<span class="hljs-string">"http://www.w3.org/1999/xhtml"</span>&gt;
&lt;head&gt;
    &lt;meta http-equiv=<span class="hljs-string">"Content-Type"</span> content=<span class="hljs-string">"text/html; charset=&lt;?= Yii::$app-&gt;charset ?&gt;"</span> /&gt;
    &lt;style type=<span class="hljs-string">"text/css"</span>&gt;
        .heading {...}
        .<span class="hljs-keyword">list</span> {...}
        .footer {...}
    &lt;/style&gt;
    <span class="hljs-preprocessor">&lt;?php</span> <span class="hljs-variable">$this</span>-&gt;head() <span class="hljs-preprocessor">?&gt;</span>
&lt;/head&gt;
&lt;body&gt;
    <span class="hljs-preprocessor">&lt;?php</span> <span class="hljs-variable">$this</span>-&gt;beginBody() <span class="hljs-preprocessor">?&gt;</span>
    <span class="hljs-preprocessor">&lt;?</span>= <span class="hljs-variable">$content</span> <span class="hljs-preprocessor">?&gt;</span>
    &lt;div <span class="hljs-class"><span class="hljs-keyword">class</span>="<span class="hljs-title">footer</span>"&gt;<span class="hljs-title">With</span> <span class="hljs-title">kind</span> <span class="hljs-title">regards</span>, &lt;?= <span class="hljs-title">Yii</span>::$<span class="hljs-title">app</span>-&gt;<span class="hljs-title">name</span> ?&gt; <span class="hljs-title">team</span>&lt;/<span class="hljs-title">div</span>&gt;
    &lt;?<span class="hljs-title">php</span> $<span class="hljs-title">this</span>-&gt;<span class="hljs-title">endBody</span>() ?&gt;
&lt;/<span class="hljs-title">body</span>&gt;
&lt;/<span class="hljs-title">html</span>&gt;
&lt;?<span class="hljs-title">php</span> $<span class="hljs-title">this</span>-&gt;<span class="hljs-title">endPage</span>() ?&gt;
</span></code></pre>
<h2>File attachment <span id="file-attachment"></span><a href="#file-attachment" class="hashlink">&para;</a></h2><p>You can add attachments to message using methods <code>attach()</code> and <code>attachContent()</code>:</p>
<pre><code class="hljs php language-php"><span class="hljs-variable">$message</span> = Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;compose();

<span class="hljs-comment">// Attach file from local file system:</span>
<span class="hljs-variable">$message</span>-&gt;attach(<span class="hljs-string">'/path/to/source/file.pdf'</span>);

<span class="hljs-comment">// Create attachment on-the-fly</span>
<span class="hljs-variable">$message</span>-&gt;attachContent(<span class="hljs-string">'Attachment content'</span>, [<span class="hljs-string">'fileName'</span> =&gt; <span class="hljs-string">'attach.txt'</span>, <span class="hljs-string">'contentType'</span> =&gt; <span class="hljs-string">'text/plain'</span>]);
</code></pre>
<h2>Embedding images <span id="embedding-images"></span><a href="#embedding-images" class="hashlink">&para;</a></h2><p>You can embed images into the message content using <code>embed()</code> method. This method returns the attachment id,
which should be then used at 'img' tag.
This method is easy to use when composing message content via view file:</p>
<pre><code class="hljs php language-php">Yii::<span class="hljs-variable">$app</span>-&gt;mailer-&gt;compose(<span class="hljs-string">'embed-email'</span>, [<span class="hljs-string">'imageFileName'</span> =&gt; <span class="hljs-string">'/path/to/image.jpg'</span>])
    <span class="hljs-comment">// ...</span>
    -&gt;send();
</code></pre>
<p>Then inside the view file you can use the following code:</p>
<pre><code class="hljs php language-php">&lt;img src=<span class="hljs-string">"&lt;?= $message-&gt;embed($imageFileName); ?&gt;"</span>&gt;
</code></pre>
<h2>Testing and debugging <span id="testing-and-debugging"></span><a href="#testing-and-debugging" class="hashlink">&para;</a></h2><p>A developer often has to check, what actual emails are sent by the application, what was their content and so on.
Such ability is granted by Yii via <code>yii\mail\BaseMailer::useFileTransport</code>. If enabled, this option enforces
saving mail message data into the local files instead of regular sending. These files will be saved under
<code>yii\mail\BaseMailer::fileTransportPath</code>, which is '@runtime/mail' by default.</p>
<blockquote class="note"><p><strong>Note: </strong>you can either save the messages to the files or send them to the actual recipients, but can not do both simultaneously.</p>
</blockquote>
<p>A mail message file can be opened by a regular text file editor, so you can browse the actual message headers, content and so on.
This mechanism may prove itself, while debugging application or running unit test.</p>
<blockquote class="note"><p><strong>Note: </strong>the mail message file content is composed via <code>\yii\mail\MessageInterface::toString()</code>, so it depends on the actual
  mail extension you are using in your application.</p>
</blockquote>
<h2>Creating your own mail solution <span id="creating-your-own-mail-solution"></span><a href="#creating-your-own-mail-solution" class="hashlink">&para;</a></h2><p>In order to create your own custom mail solution, you need to create 2 classes: one for the 'Mailer' and
another one for the 'Message'.
You can use <code>yii\mail\BaseMailer</code> and <code>yii\mail\BaseMessage</code> as the base classes for your solution. These classes
already contain the basic logic, which is described in this guide. However, their usage is not mandatory, it is enough
to implement <code>yii\mail\MailerInterface</code> and <code>yii\mail\MessageInterface</code> interfaces.
Then you need to implement all the abstract methods to build your solution.</p>
        <div class="toplink"><a href="#" class="h1" title="go to top"><span class="glyphicon glyphicon-arrow-up"></a></div>
    </div>
</div>


</div>

<footer class="footer">
        <p class="pull-right"><small>Page generated on Sat, 09 Jul 2016 12:16:28 +0000</small></p>
    Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a></footer>

<script type="text/javascript">jQuery(document).ready(function () {
    var shiftWindow = function () { scrollBy(0, -50) };
    if (location.hash) setTimeout(shiftWindow, 1);
    window.addEventListener("hashchange", shiftWindow);
var element = document.createElement("script");
element.src = "./jssearch.index.js";
document.body.appendChild(element);

var searchBox = $('#searchbox');

// search when typing in search field
searchBox.on("keyup", function(event) {
    var query = $(this).val();

    if (query == '' || event.which == 27) {
        $('#search-resultbox').hide();
        return;
    } else if (event.which == 13) {
        var selectedLink = $('#search-resultbox a.selected');
        if (selectedLink.length != 0) {
            document.location = selectedLink.attr('href');
            return;
        }
    } else if (event.which == 38 || event.which == 40) {
        $('#search-resultbox').show();

        var selected = $('#search-resultbox a.selected');
        if (selected.length == 0) {
            $('#search-results').find('a').first().addClass('selected');
        } else {
            var next;
            if (event.which == 40) {
                next = selected.parent().next().find('a').first();
            } else {
                next = selected.parent().prev().find('a').first();
            }
            if (next.length != 0) {
                var resultbox = $('#search-results');
                var position = next.position();

//              TODO scrolling is buggy and jumps around
//                resultbox.scrollTop(Math.floor(position.top));
//                console.log(position.top);

                selected.removeClass('selected');
                next.addClass('selected');
            }
        }

        return;
    }
    $('#search-resultbox').show();
    $('#search-results').html('<li><span class="no-results">No results</span></li>');

    var result = jssearch.search(query);

    if (result.length > 0) {
        var i = 0;
        var resHtml = '';

        for (var key in result) {
            if (i++ > 20) {
                break;
            }
            resHtml = resHtml +
            '<li><a href="' + result[key].file.u.substr(3) +'"><span class="title">' + result[key].file.t + '</span>' +
            '<span class="description">' + result[key].file.d + '</span></a></li>';
        }
        $('#search-results').html(resHtml);
    }
});

// hide the search results on ESC
$(document).on("keyup", function(event) { if (event.which == 27) { $('#search-resultbox').hide(); } });
// hide search results on click to document
$(document).bind('click', function (e) { $('#search-resultbox').hide(); });
// except the following:
searchBox.bind('click', function(e) { e.stopPropagation(); });
$('#search-resultbox').bind('click', function(e) { e.stopPropagation(); });

});</script></body>
</html>
